package com.lzh.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.base.handle.MyException;
import com.lzh.commonutils.JwtUtils;
import com.lzh.ucenter.entity.Member;
import com.lzh.ucenter.mapper.MemberMapper;
import com.lzh.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.ucenter.utils.MD5Util;
import com.lzh.ucenter.vo.LoginVo;
import com.lzh.ucenter.vo.RegisterVo;
import com.lzh.ucenter.vo.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lizuhu
 * @since 2021-05-19
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录
    @Override
    public String login(LoginVo login) {
        //获取手机号
        String mobile = login.getMobile();
        String password = login.getPassword();
        //判断手机号和密码是否为空
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)) {
            throw new MyException(404,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member member = baseMapper.selectOne(wrapper);
        if (member == null) {
            throw new MyException(400,"手机号不存在！");
        }
        //判断密码
        if (!MD5Util.encrypt(password).equals(member.getPassword())) {
            throw new MyException(400,"输入的密码不正确！");
        }
        //判断用户是否被禁用
        if (member.getIsDisabled()) {
            throw new MyException(400,"用户已禁用！");
        }

        //登录成功后生成token
        String token = JwtUtils.createToken(member.getId(), member.getNickname());
        return token;
    }
    //注册用户
    @Override
    public boolean register(RegisterVo register) {
        //得到提交的数据
        String nickname = register.getNickname();
        String mobile = register.getMobile();
        String password = register.getPassword();
        String code = register.getCode();
        //判断是否为空
        if (StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)) {
            throw new MyException(400,"输入的数据有误，注册失败!");
        }
        //校验验证码
        String mobileCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobileCode)) {
            throw new MyException(400,"验证码错误！");
        }
        //查找是否存在该手机号用户
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count.intValue() > 0) {
            throw new MyException(400,"用户已存在！注册失败！");
        }
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5Util.encrypt(password));
        member.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/666ZxTwTYHUCf6c2HdttOkdzBD3YAq6Mp3Mpf8eB61jzfvHNSucUx3C7jicsuNXjxPCRXkzydekmRibziaoPibibWkA/132");
        boolean save = this.save(member);
        if (save) return true;
        else return false;
    }
    //根据token值获取用户
    @Override
    public UserInfo getUserInfo(String userId) {
        Member user = baseMapper.selectById(userId);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user,userInfo);
        return userInfo;
    }

    @Override
    public Member getUserByOpenId(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Member user = baseMapper.selectOne(wrapper);
        return user;
    }
}
